<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/6/22
 * Time: 13:42
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Sailimg extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('match_sail_image')->where('msi_status', 1)->count();// 获取总条数
            $lists = Db::name('match_sail_image')
                ->field('msi_id, msi_name, msi_image, msi_time, msi_num, msi_raobiao, msi_biao_name')
                ->where('msi_status', 1)
                ->page($Nowpage,$limits)
                ->order('msi_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加航线图
     * @return mixed|\think\response\Json
     */
    public function sailAdd(){
        if(Request::instance()->isPost()){
            $msi_name = trim(input('msi_name'), ' ');
            $msi_image = input('msi_image');
            $msi_num = input('msi_num');
            $msi_raobiao = trim(input('msi_raobiao'), ' ');
            $msi_biao_name = trim(input('msi_biao_name'), ' ');
            $insert = [
                'msi_name' => $msi_name,
                'msi_image' => $msi_image,
                'msi_num' => $msi_num,
                'msi_time' => date('Y-m-d H:i:s'),
                'msi_raobiao' => $msi_raobiao,
                'msi_biao_name' => $msi_biao_name
            ];
            $res = Db::table('up_match_sail_image')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('sailimg/sailadd');
    }
    
    /**
     * 编辑航线图
     * @return mixed|\think\response\Json
     */
    public function sailEdit(){
        $msi_id = input('msi_id');
        if(Request::instance()->isPost()){
            $msi_name = trim(input('msi_name'), ' ');
            $msi_image = input('msi_image');
            $msi_num = input('msi_num');
            $msi_raobiao = trim(input('msi_raobiao'), ' ');
            $msi_biao_name = trim(input('msi_biao_name'), ' ');
            $update = [
                'msi_name' => $msi_name,
                'msi_num' => $msi_num,
                'msi_raobiao' => $msi_raobiao,
                'msi_biao_name' => $msi_biao_name
            ];
            if(!empty($msi_image)){
                $update['msi_image'] = $msi_image;
            }
            $res = Db::table('up_match_sail_image')->where('msi_id', $msi_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $sail = Db::table('up_match_sail_image')
            ->field('msi_name, msi_image, msi_num, msi_raobiao, msi_biao_name')
            ->where('msi_id', $msi_id)
            ->find();
        $this->assign([
            'sail' => $sail,
            'msi_id' => $msi_id
        ]);
        return $this->fetch('sailimg/sailedit');
    }
    
    /**
     * 删除航线图
     * @return \think\response\Json
     */
    public function sailDel(){
        $msi_id = input('id');
        $res = Db::table('up_match_sail_image')->where('msi_id', $msi_id)->setField('msi_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}